// pages/zhuye/zhuye.js
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */

  data: {
    show_a: '',
    phone: '',
    jiami: '',
    OPEN_ID: '',
    session_key: '',
    Getphone: true,
  },

  /**
   * 图片点击跳转
   */


  //渭南员工
  weinanyuan() {
    wx.navigateTo({
      url: '../../pages/weinanyuangongdenglu/weinanyuangongdenglu'
    })
  },




  //西安员工
  xianyuan: function () {
    wx.navigateTo({
      url: '../../pages/xianyuangongdenglu/xianyuangongdenglu'
    })
  },

  //汉中员工登录
  huanzhong: function () {
    wx.navigateTo({
      url: '../../pages/hanzhongyuangongdeng/hanzhongyuangongdeng'
    })
  },




  //安康员工登录
  user_ak: function () {
    wx.navigateTo({
      url: '../../pages/akyuanlong/akyuanlong'

    })
  },


  //商洛员工登录
  user_c: function () {
    wx.navigateTo({
      url: '../../pages/shangyuan/shangyuan'

    })
  },




  //省公司登录
  user: function (e) {

    console.log(e);
    wx.navigateTo({
      url: '../../pages/login/login'
    })

  },

  //宝鸡员工登录
  user_bj: function () {
    wx.navigateTo({
      url: '../../pages/bjygdenglu/bjygdenglu'
    })
  },



  //抽取代金券
  // user_b:function(){
  //     wx.navigateTo({
  //       url: '../../pages/jqlogin/jqlogin'
  //     })
  //   },

  //商洛大抽奖
  user_e: function () {
    wx.navigateTo({
      url: '../../pages/yinliushuju/yinliushuju'
    })
  },

  //咸阳登录
  user_xy: function () {
    wx.navigateTo({
      url: '../../pages/xyyuanlogin/xyyuanlogin'
    })
  },

  //铜川登陆
  user_tc: function () {
    wx.navigateTo({
      url: '../../pages/tcyuanlogin/tcyuanlogin'
    })
  },

  shang_hj: function () {
    wx.navigateTo({
      url: '../../pages/shanghjzc/shanghjzc'
    })
  },

  //获取手机号
  getPhoneNumber(e) {
    console.log(e);
    // console.log(e.detail.code)
    this.setData({
      code: e.detail.code
    })
    wx.request({
      url: app.globalData.interface + '/post/API_1032_getWeiXinPhone',
      method: 'GET',
      //向后端发送的数据
      data: {
        code: this.data.code //将code发送到后台服务器。
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded" //POST方式是这个
      },
      success: res => {
        console.log(res);
        console.log(res.data.phone);
        if (res.data.result != 'success') {
          wx.showToast({
            title: '获取手机号失败',
            duration: 1000
          })
        } else {
          this.setData({
            phone: res.data.phone,
            Getphone: true,
          })
          this.baocunphone() //保存手机号接口


        }
      }
    })
  },

  baocunphone() {
    wx.request({
      url: app.globalData.interface + '/newVersion/API_2011_savePhoneAndOpenId',
      method: 'POST',
      //向后端发送的数据
      data: {
        CUSTOMER_TEL: this.data.phone,

        KEY: 'xianjgjKey9999',
        OPEN_UUID: this.data.jiami,
        OPEN_ID: this.data.openid,
        session_key: this.data.session_key,
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded" //POST方式是这个
      },
      success: res => {
        console.log(res);
      }
    })

  },




  // 判断是否授权
  // scanCod() {
  //   var that = this
  //   wx.request({ 
  //   url: app.globalData.interface+'/newVersion/API_2007_getPhoneByOpenId',
  //   method: 'POST',
  //   //向后端发送的数据
  //   data: {
  //     OPEN_ID:this.data.openid  //将code发送到后台服务器。
  //   },  
  //   header: {
  //     "Content-Type": "application/x-www-form-urlencoded"  //POST方式是这个
  //   },
  //   success:res => { 
  //     console.log(res);
  //     console.log(res.data.data.CUSTOMER_TEL);
  //     if (res.data.result != 'success') {
  //       wx.showToast({
  //         title: '请获取手机号',
  //         duration: 1000
  //       })
  //       that.setData({

  //         Getphone: false,
  //       })

  //     } else {
  //       that.setData({
  //         LEVEL_IMG:res.data.data.LEVEL_IMG,
  //         CUSTOMER_TEL:res.data.data.CUSTOMER_TEL,
  //         Getphone: true,
  //       })

  //     }
  //   }
  // })
  // },

  shentiaozhuan() {
    var that = this
    wx.request({
      url: app.globalData.interface + '/management/API3_1019_EmployeeLogo',
      method: 'POST',
      //向后端发送的数据
      data: {
        OPEN_ID: this.data.openid, //用户OPEN_ID
        session_key: this.data.session_key,
        OPEN_UUID: this.data.jiami
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded" //POST方式是这个
      },
      success: res => {
        console.log(res.data.data.L1code);
        let mealsList = res.data.data.EMPLOYEE_ID;
        app.globalData.mealsList = mealsList;
        if (res.data.result != 'success') {

          wx.showToast({
            title: res.data.data,
            duration: 2000
          })
          console.log(22);
        } else {

          that.setData({
            L2code: res.data.data.L2code
          })

          wx.navigateTo({
            url: '../../pages/daijin/daijin'
          })



        }
      }
    })

  },





  panduanyuan() {
    var that = this
    wx.request({
      url: app.globalData.interface + '/management/API3_1019_EmployeeLogo',
      method: 'POST',
      //向后端发送的数据
      data: {
        OPEN_ID: this.data.openid, //用户OPEN_ID
        session_key: this.data.session_key,
        OPEN_UUID: this.data.jiami
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded" //POST方式是这个
      },
      success: res => {
        console.log(res.data.data.L1code);

        let mealsList = res.data.data.EMPLOYEE_ID;
        app.globalData.mealsList = mealsList;

        if (res.data.result != 'success') {

          wx.showToast({
            title: res.data.data,
            duration: 2000
          })
          console.log(22);
        } else {

          that.setData({
            L2code: res.data.data.L2code
          })

          if (res.data.data.L2code == 61000089) {
            wx.showToast({
              title: '您暂无登录权限',
              duration: 2000
            })
          } else if (res.data.data.L2code == 61012777) {
            wx.navigateTo({
              url: '../../pages/xianyuangongdengluxuanze/xianyuangongdengluxuanze'
            })
          } else if (res.data.data.L2code == 61000014) {
            wx.navigateTo({
              url: '../../pages/hanzhongyuangongtiaozhuan/hanzhongyuangongtiaozhuan'
            })
          } else if (res.data.data.L2code == 61000103) {
            wx.navigateTo({
              url: '../../pages/ankangygtiaozhuan/ankangygtiaozhuan'
            })
          } else if (res.data.data.L2code == 61000127) {
            wx.navigateTo({
              url: '../../pages/gxtiaoz/gxtiaoz'
            })
          } else if (res.data.data.L2code == 61000077) {
            wx.navigateTo({
              url: '../../pages/xytiaoz/xytiaoz'
            })
          } else if (res.data.data.L2code == 61000065) {
            wx.navigateTo({
              url: '../../pages/tctiaoz/tctiaoz'
            })
          } else if (res.data.data.L2code == 61000026) {
            wx.navigateTo({
              url: '../../pages/shangtiaoz/shangtiaoz'
            })
          } else if (res.data.data.L2code == 61000091) {
            wx.navigateTo({
              url: '../../pages/weinanyuangongtiaozhuanye/weinanyuangongtiaozhuanye'
            })
          } else if (res.data.data.L2code == 61000040) {

            wx.showToast({
              title: '此区域暂无活动',
              duration: 2000
            })

          } else if (res.data.data.L2code == 61000038) {

            wx.showToast({
              title: '此区域暂无活动',
              duration: 2000
            })

          }


        }
      }
    })

  },


  panduanyuanaaaa() {
    var that = this
    wx.request({
      url: app.globalData.interface + '/management/API3_1019_EmployeeLogo',
      method: 'POST',
      //向后端发送的数据
      data: {
        OPEN_ID: this.data.openid, //用户OPEN_ID
        session_key: this.data.session_key,
        OPEN_UUID: this.data.jiami
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded" //POST方式是这个
      },
      success: res => {
        console.log(res.data.data.L1code);

        if (res.data.result != 'success') {

        wx.showToast({
          title: res.data.data,
          duration: 2000
        })
          console.log(22);
        } else {
 
          that.setData({
            L2code:res.data.data.L2code
          })

   

      

        }
      }
    })

  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    var that = this


    that.setData({
      phone: app.globalData.phone,

      openid: app.globalData.openid,
      jiami: app.globalData.jiami,
      OPEN_ID: app.globalData.openid,
      session_key: app.globalData.session_key,
    });






  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {


    setTimeout(() => {
      this.setData({
        phone: app.globalData.phone,

        openid: app.globalData.openid,
        jiami: app.globalData.jiami,
        OPEN_ID: app.globalData.openid,
        session_key: app.globalData.session_key,
      });

      console.log(this.data.phone);


      var that = this

      if (that.data.phone == '') {

        wx.login({
          success: function (res) {
            console.log(res);
            if (res.code) { //wx.login获取code。
              console.log(res.code);
              //发起网络请求
              wx.request({
                url: app.globalData.interface + '/post/API_1062_appGetOpenid',
                method: 'GET',
                //向后端发送的数据
                data: {
                  code: res.code //将code发送到后台服务器。
                },
                header: {
                  "Content-Type": "application/x-www-form-urlencoded" //POST方式是这个
                },
                success: res => {
                  console.log(res);
                  console.log(res.data.openid);
                  let openid = res.data.openid;
                  app.globalData.openid = openid;
                  that.setData({
                    openid: res.data.openid
                  })
                  // that.scanCod()
                }
              })
            } else {
              console.log('获取用户登录态失败！' + res.errMsg)
            }
          }
        });
        that.panduanyuanaaaa()
      } else {
        that.panduanyuanaaaa()
        console.log('已获取');
      }
    }, 1000)








    wx.request({
      url: app.globalData.interface + '/shanyouhui/API_1010_getOneConfig?code=LOOKBANNER',
      method: 'GET',

      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        console.log(res.data.configList);

        this.setData({
          show_a: res.data.configList //on
        })
      }
    })







  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }

})